package com.aa.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.aa.Common;
import com.aa.R;
import com.aa.http.HttpOperation;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ChangePasswordActivity extends Activity {
	private final String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	private String TAG = "ChangePasswordActivity";
	private TextView mTitleView;
	private EditText numberText;
	private EditText oldPasswordText;
	private EditText passwordText;
	private EditText passwordText2;
	private ProgressDialog progressDialog = null;
	private Button title_btn_back;
	private Button title_btn_ok;
	private Handler mHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepassword);
		initView();
		initListener();
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
					Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_9, 3000).show();
					return;
				case 1:
					Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_8, 3000).show();
					Common.saveUserInfo(ChangePasswordActivity.this);
					finish();
					return;
				case 2:
					Toast.makeText(ChangePasswordActivity.this, R.string.balance_2, 3000).show();
				default:
					break;
				}
			}
			
		};
	}
	
	private void initView(){
		mTitleView = ((TextView)findViewById(R.id.app_title_center));
	    mTitleView.setText(R.string.changepassword_title);
	    numberText = ((EditText)findViewById(R.id.changepassword_edit0));
	    numberText.setText(Common.iMyPhoneNumber);
	    oldPasswordText = ((EditText)findViewById(R.id.changepassword_edit1));
	    passwordText = ((EditText)findViewById(R.id.changepassword_edit2));
	    passwordText2 = ((EditText)findViewById(R.id.changepassword_edit3));
	    title_btn_back = ((Button)findViewById(R.id.title_btn1));
	    title_btn_back.setVisibility(1);
	    title_btn_back.setText(R.string.app_back_tip);
	    title_btn_ok = ((Button)findViewById(R.id.title_btn4));
	    title_btn_ok.setVisibility(1);
	    title_btn_ok.setText(R.string.app_ok_tip);
	}
	
	private void initListener(){
		title_btn_back.setOnClickListener(new OnChangePasswordClickListener());
		title_btn_ok.setOnClickListener(new OnChangePasswordClickListener());
	}
	
	private class OnChangePasswordClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_btn1:
				finish();
				break;
			case R.id.title_btn4:
				changepassword();
			default:
				break;
			}
			
		}
		
	}
	
	private void changepassword()
	  {
	    String str1 = oldPasswordText.getText().toString();
	    if (str1.length() == 0)
	    {
	    
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_5, 3000).show();
	      return;
	    }
	    if (str1.length() > 16 )
	    {
	     
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_6, 3000).show();
	      return;
	    }
	    String str2 = passwordText.getText().toString();
	    if (str2.length() == 0)
	    {
	      
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_5, 0).show();
	      return;
	    }
	    if (str2.length() > 16 )
	    {
	    	Toast.makeText(getApplicationContext(), R.string.changepassword_6, 0).show();
	    	return;
	    }
	  
	    String str3 = passwordText2.getText().toString();
	    if (str3.length() == 0)
	    {
	     
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_5, 3000).show();
	      return;
	    }
	    if (str3.length() > 16 )
	    {
	     
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_6, 0).show();
	      return;
	    }
	    if (!str2.equals(str3))
	    {	      
	      Toast.makeText(ChangePasswordActivity.this, R.string.changepassword_7, 0).show();
	      return;
	    }
	    
	    submitChangePwd(str1, str2);
	  }
	
	private void submitChangePwd(String pOldPwd,final String pNewPwd){
		 progressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), getString(R.string.app_progress_tip), true, false);
		 progressDialog.setCancelable(true);
		 
		 

			final JSONObject _SendJson = new JSONObject();
			JSONObject _BodyJson = new JSONObject();
			JSONObject _HeadJson = new JSONObject();
			try {
				_HeadJson.put("v", Common.iVersion);
				_HeadJson.put("time", System.currentTimeMillis());
				_HeadJson.put("cmd", 4);
				_HeadJson.put("mac", Common.iMac);
				
				
		
					_BodyJson.put("pwd", pOldPwd);
					_BodyJson.put("newpwd", pNewPwd);
					_BodyJson.put("uid", Common.iUID);
		
				
				_SendJson.put("head", _HeadJson);
				_SendJson.put("body", _BodyJson);
				Log.i(TAG, "_SendJson = " + _SendJson.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			new Thread(new Runnable() {				
				@Override
				public void run() {
					String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
					progressDialog.dismiss();
					Log.i(TAG, "submitChangePwd = " + _Result);
					if (_Result == null || _Result.length() == 0 || _Result.equals("ConnectTimeout")) {
						mHandler.sendEmptyMessage(2);
						return;
					}
					
					try {
						JSONObject _JsonObject = new JSONObject(_Result); 
						JSONObject _Body = _JsonObject.getJSONObject("body");
					
						if ((_Body.getInt("rcode")) == 1) {												
								
								Common.iPassword = pNewPwd;	
								mHandler.sendEmptyMessage(1);
						}else {
							mHandler.sendEmptyMessage(0);
						}
						
					} catch (JSONException e) {
					
						e.printStackTrace();
						Log.i(TAG, "submitChangePwd fail");
					}
					
				}
			}).start();
	}
}
